﻿@model GameWeb.Models.GameMaintenanceModel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm()) {

<input type="button" value="New Game" id="btnNewGame" />

<br />
    
<p />
    
<div id="divCurrentGames">
    <table>
        @foreach (var match in Model.GameMatches)
        { 
        <tr>
            <td><a href="/Game/Play/@match.GameMatchId"> @match.GameMatchId</a></td>
            <td><a href="/Game/Play/@match.GameMatchId"> @match.PlayerDisplay</a></td>
        </tr>
        }
    </table>
</div>

<div id="divPlayerSelect">

</div>
<div id="divNewGame" style="display:none;height:400px;width:600px">
@Html.Partial("PlayerSelect", Model.OtherPlayers)
<br />

</div>

<script type="text/javascript">

    $(document).ready(function () {
        $('#btnNewGame').click(function () {
            $('#divNewGame').dialog('open');
        });

        $('#btnCreateGame').click(function () {
            //GameMaintenance.PostForm("/GameMaintenance/CreateGame", function (resp) {
            //    alert("post ok");
            //},
            //function (err) {
            //});

            var pd = $("form").serialize();
            var frm = $("form");
            var n = 5;

        });

        $('form').submit(function () {
            alert($(this).serialize());
            return false;
        });
        $('#divNewGame').dialog({
            autoOpen: false,
            modal: true,
            title: 'New Game',
            open: function (type, data) {
                $(this).parent().appendTo("form");
            },
            buttons: {
                "Start": function () {
                    $(this).dialog('close');
                    var pd = $("form").serialize();
                    GameMaintenance.PostForm("/GameMaintenance/CreateGame", function (resp) {
                        //do response from server
                        var matchId = resp.matchId;
                        window.location.href = "/Game/Play/" + matchId;
                    }, function (err) { });
                },
                "Cancel": function () {
                    $(this).dialog('close');
                }
            }
        });
    });
</script>

}